home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / lha101u / src / lhlist.c < prev    next >
C/C++ Source or Header  |  1992-05-02  |  8KB  |  313 lines

  1. /*----------------------------------------------------------------------*/
  2. /*        LHarc List Command                    */
  3. /*        This is part of LHarc UNIX Archiver Driver        */
  4. /*                                    */
  5. /*        Copyright(C) MCMLXXXIX  Yooichi.Tagawa            */
  6. /*                                    */
  7. /*  V0.00  Original                1988.05.23  Y.Tagawa    */
  8. /*  V1.00  Fixed                1989.09.22  Y.Tagawa    */
  9. /*  V1.01  Bug Fix for month name        1989.12.25  Y.Tagawa    */
  10. /*----------------------------------------------------------------------*/
  11.  
  12. #include "lharc.h"
  13.  
  14. static long packed_size_total;
  15. static long original_size_total;
  16. static int list_files;
  17.  
  18. /*----------------------------------------------------------------------*/
  19. /*                Print Stuff                */
  20. /*----------------------------------------------------------------------*/
  21.  
  22. /* need 14 or 22 (when verbose_listing is TRUE) column spaces */
  23. static void
  24. print_size (packed_size, original_size)
  25.      long packed_size, original_size;
  26. {
  27.   if (verbose_listing)
  28.     printf ("%7d ", packed_size);
  29.  
  30.   printf ("%7d ", original_size);
  31.  
  32.   if (original_size == 0L)
  33.     printf ("******");
  34.   else
  35.     printf ("%3d.%1d%%",
  36.         (int)((packed_size * 100L) / original_size),
  37.         (int)((packed_size * 1000L) / original_size) % 10);
  38. }
  39.  
  40. /* need 12 or 17 (when verbose_listing is TRUE) column spaces */
  41. static void
  42. print_stamp (t)
  43.      time_t t;
  44. {
  45.   static boolean got_now = FALSE;
  46.   static time_t now;
  47.   static unsigned int threshold;
  48.   static char t_month[12*3+1] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  49.   struct tm *p;
  50.  
  51.   if (t == 0)
  52.     {
  53.     printf ("            "); /* 12 spaces */
  54.       return;
  55.     }
  56.  
  57.   if (!got_now)
  58.     {
  59.       now = time ((time_t*)0);
  60.       p = localtime (&now);
  61.       threshold = p->tm_year * 12 + p->tm_mon - 6;
  62.       got_now = TRUE;
  63.     }
  64.  
  65.   p = localtime (&t);
  66.  
  67.   if (p->tm_year * 12 + p->tm_mon > threshold)
  68.     printf ("%.3s %2d %02d:%02d",
  69.         &t_month[p->tm_mon * 3], p->tm_mday, p->tm_hour, p->tm_min);
  70.   else
  71.     printf ("%.3s %2d  %04d",
  72.         &t_month[p->tm_mon * 3], p->tm_mday, p->tm_year + 1900);
  73. }
  74.  
  75. static void
  76. print_bar ()
  77. {
  78.   char *p, *q;
  79.   /* 17+1+(0 or 7+1)+7+1+6+1+(0 or 1+4)+(12 or 17)+1+20 */
  80.   /*       12345678901234567_  1234567_123456  _123456789012   1234 */
  81.  
  82.   if (verbose_listing)
  83.     {
  84.       p = "- ------ ---------- ";
  85.       q = " -------------";
  86.     }
  87.   else
  88.     {
  89.       p = " ";
  90.       q = " --------------------";
  91.     }
  92.  
  93.   if (verbose)
  94.     q = "";
  95.  
  96.   printf ("----------------- ------- ------%s------------%s\n", p, q);
  97. }
  98.  
  99.  
  100. /*----------------------------------------------------------------------*/
  101. /*                                    */
  102. /*----------------------------------------------------------------------*/
  103.  
  104. static void
  105. list_header ()
  106. {
  107.   char *p, *q;
  108.  
  109.   if (verbose_listing)
  110.     {
  111.       p = "PACKED    SIZE  RATIO        CRC   ";
  112.       q = "       NAME";
  113.     }
  114.   else
  115.     {
  116.       p = "  SIZE  RATIO";
  117.       q = "    NAME";
  118.     }
  119.  
  120.   if (verbose)
  121.     q = "";
  122.  
  123.   printf (" PERMSSN  UID GID  %s     STAMP%s\n", p, q);
  124. #if 0
  125.   printf (" PERMSSN  UID GID %s   SIZE  RATIO%s %s    STAMP%s%s\n",
  126.       verbose_listing ? " PACKED " : "",    /* 8,0 */
  127.       verbose_listing ? "  CRC" : "",    /* 5,0 */
  128.       verbose_listing ? "  " : "",        /* 2,0 */
  129.       verbose_listing ? "      " : "   ",        /* 6,3 */
  130.       verbose ? "" : " NAME");
  131. #endif
  132.   print_bar ();
  133. }
  134.  
  135. static void
  136. list_one (hdr)
  137.      register LzHeader *hdr;
  138. {
  139.   register int mode;
  140.   register char *p;
  141.   char method[6];
  142.   char modebits[10];
  143.  
  144.   if (verbose)
  145.     printf ("%s\n", hdr->name);
  146.  
  147.   strncpy(method,hdr->method,5);
  148.   method[5]='\0';
  149.  
  150.   switch ( mode=hdr->extend_type )
  151.   {
  152.       case EXTEND_UNIX:
  153.         mode=hdr->unix_mode;
  154.       modebits[0]=((mode & UNIX_OWNER_READ_PERM)  ? 'r' : '-');
  155.       modebits[1]=((mode & UNIX_OWNER_WRITE_PERM) ? 'w' : '-');
  156.       if (mode & UNIX_SETUID)
  157.         modebits[2]='s';
  158.       else
  159.         modebits[2]=((mode & UNIX_OWNER_EXEC_PERM) ? 'x' : '-');
  160.       modebits[3]=((mode & UNIX_GROUP_READ_PERM)  ? 'r' : '-');
  161.       modebits[4]=((mode & UNIX_GROUP_WRITE_PERM) ? 'w' : '-');
  162.       if (mode & UNIX_SETGID)
  163.         modebits[5]='s';
  164.       else
  165.         modebits[5]=((mode & UNIX_GROUP_EXEC_PERM) ? 'x' : '-');
  166.       modebits[6]=((mode & UNIX_OTHER_READ_PERM)  ? 'r' : '-');
  167.       modebits[7]=((mode & UNIX_OTHER_WRITE_PERM) ? 'w' : '-');
  168.       if (mode & UNIX_STYCKYBIT)    /* STICKYBIT */
  169.         modebits[8]='t';
  170.       else
  171.           modebits[8]=((mode & UNIX_OTHER_EXEC_PERM)  ? 'x' : '-');
  172.       modebits[9]='\0';
  173.       printf ("%s%4d/%-4d", modebits, hdr->unix_uid, hdr->unix_gid);
  174.       break;
  175.     case EXTEND_OS68K:
  176. /**/    case EXTEND_XOSK: /**/
  177.         mode=hdr->unix_mode;
  178.       printf ("%c%c%c%c%c%c%c%c %4d/%-4d",
  179.           ((mode & OSK_DIRECTORY_PERM)    ?    'd' : '-'),
  180.           ((mode & OSK_SHARED_PERM)     ?     's' : '-'),
  181.           ((mode & OSK_OTHER_EXEC_PERM)    ?     'e' : '-'),
  182.           ((mode & OSK_OTHER_WRITE_PERM)?     'w' : '-'),
  183.           ((mode & OSK_OTHER_READ_PERM)    ?     'r' : '-'),
  184.           ((mode & OSK_OWNER_EXEC_PERM)    ?     'e' : '-'),
  185.           ((mode & OSK_OWNER_WRITE_PERM)?     'w' : '-'),
  186.           ((mode & OSK_OWNER_READ_PERM)    ?     'r' : '-'),
  187.           hdr->unix_uid, hdr->unix_gid);
  188.       break;
  189.     default:
  190.       switch (hdr->extend_type)
  191.         {            /* max 18 characters */
  192.         case EXTEND_GENERIC:p = "[generic]"; break;
  193.         case EXTEND_CPM:    p = "[CP/M]"; break;
  194.         case EXTEND_FLEX:    p = "[FLEX]"; break;
  195.         case EXTEND_OS9:    p = "[OS-9]"; break;
  196.         case EXTEND_OS68K:    p = "[OS-9/68K]"; break;
  197.         case EXTEND_MSDOS:    p = "[MS-DOS]"; break;
  198.         case EXTEND_MACOS:    p = "[Mac OS]"; break;
  199.         case EXTEND_OS2:    p = "[OS/2]"; break;
  200.         case EXTEND_HUMAN:    p = "[Human68K]"; break;
  201.         case EXTEND_OS386:    p = "[OS-386]"; break;
  202.         case EXTEND_RUNSER:    p = "[Runser]"; break;
  203. #ifdef EXTEND_TOWNSOS
  204.           /* This ID isn't fixed */
  205.         case EXTEND_TOWNSOS:    p = "[TownsOS]"; break;
  206. #endif
  207.           /* Ouch!  Please customize it's ID.  */
  208.         default:         p = "[unknown]"; break;
  209.         }
  210.       printf ("%-18.18s", p);
  211.       break;
  212.     }
  213.   
  214.   print_size (hdr->packed_size, hdr->original_size);
  215.  
  216.   if (verbose_listing)
  217.     if (hdr->has_crc)
  218.       printf (" %s %04x", method,hdr->crc);
  219.     else
  220.       printf (" %s ****",method);
  221.   
  222.   printf (" ");
  223.   print_stamp (hdr->unix_last_modified_stamp);
  224.  
  225.   if (!verbose)
  226.     printf (" %s", hdr->name);
  227.  
  228.   printf ("\n");
  229. }
  230.  
  231. static void
  232. list_tailer ()
  233. {
  234.   struct stat stbuf;
  235.  
  236.   print_bar ();
  237.  
  238.   printf (" Total %4d file%c ",
  239.       list_files, (list_files == 1) ? ' ' : 's');
  240.   print_size (packed_size_total, original_size_total);
  241.   printf (" ");
  242.  
  243.   if (verbose_listing)
  244.     printf ("           ");
  245.  
  246.   if (stat (archive_name, &stbuf) < 0)
  247.     print_stamp ((time_t)0);
  248.   else
  249.     print_stamp (stbuf.st_mtime);
  250.  
  251.   printf ("\n");
  252. }
  253.  
  254. /*----------------------------------------------------------------------*/
  255. /*        LIST COMMAND MAIN                    */
  256. /*----------------------------------------------------------------------*/
  257.  
  258. void
  259. cmd_list ()
  260. {
  261.   FILE *afp;
  262.   LzHeader hdr;
  263.   int i;
  264.  
  265.   /* initialize total count */
  266.   packed_size_total = 0L;
  267.   original_size_total = 0L;
  268.   list_files = 0;
  269.  
  270.   /* open archive file */
  271.   if ((afp = open_old_archive ()) == NULL)
  272.     fatal_error (archive_name);
  273.   if (archive_is_msdos_sfx1 (archive_name))
  274.     skip_msdos_sfx1_code (afp);
  275.  
  276.   /* print header message */
  277.   if (!quiet)
  278.     list_header ();
  279.  
  280.   /* print each file information */
  281.   while (get_header (afp, &hdr))
  282.     {
  283.       if (need_file (hdr.name))
  284.     {
  285.       list_one (&hdr);
  286.       list_files ++;
  287.       packed_size_total += hdr.packed_size;
  288.       original_size_total += hdr.original_size;
  289.     }
  290.  
  291.       if (afp != stdin)
  292.       fseek (afp, hdr.packed_size, SEEK_CUR);
  293.       else
  294.     {
  295.       i = hdr.packed_size;
  296.       while(i--) fgetc(afp);
  297.     }
  298.     }
  299.  
  300.   /* close archive file */
  301.   fclose (afp);
  302.  
  303.   /* print tailer message */
  304.   if (!quiet)
  305.     list_tailer ();
  306.  
  307.   return;
  308. }
  309.  
  310.  
  311.  
  312.  
  313.